package com.z.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.z.sys.entity.Festival;
import com.z.common.web.base.BaseController;
import com.z.common.web.vo.request.PageVo;
import com.z.common.aop.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import com.z.common.aop.enums.BusinessType;
import com.z.common.web.vo.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.z.sys.service.IFestivalService;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.util.DateUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 节假日管理Controller
 *
 * @author z
 * @date 2023-07-18
 */
@Api(tags = "节假日管理")
@RestController
@RequestMapping("/system/festival")
public class FestivalController extends BaseController {

    private String prefix = "system/festival";

    @Autowired
    private IFestivalService festivalService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/sys/festival/main','sys:festival:main')")
    @ApiOperation(value = "查询页面")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询节假日管理列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/sys/festival/data','sys:festival:main')")
    @Log(title = "节假日管理", describe = "查询节假日管理列表", type = BusinessType.QUERY)
    @ApiOperation(value = "查询接口")
    public Result list(@ModelAttribute Festival festival, PageVo pageVo) {
        PageInfo<Festival> pageInfo = festivalService.selectFestivalPage(festival, pageVo);
        return Result.table(pageInfo);
    }
    /**
     *
     * @param festival
     * @return
     */
    @ResponseBody
    @GetMapping("/mark")
    @Log(title = "节假日", describe = "查询节假日日历标记", type = BusinessType.QUERY)
    public Result mark(@ModelAttribute Festival festival) {
        int year = DateUtils.year(new Date());
        List<Festival> list = festivalService.list(new QueryWrapper<Festival>().between("year", year-1, year+1));
        StringBuilder str = new StringBuilder("{");
        for(Festival f : list){

            String work = f.getWorkDate();
            String rest = f.getRestDate();
            if(StringUtils.isNotEmpty(work)){
                String[] works = work.split(",");
                for(String w: works){

                    str.append( "\"" + w + "\":\"班\",");
                }
            }
            if(StringUtils.isNotEmpty(rest)){
                String[] rests = rest.split(",");
                for(String r: rests){

                    str.append( "\"" + r + "\":\"休\",");
                }
            }
        };
        return Result.success(1, str.toString().substring(0, str.length()-1) + "}");
    }
    /**
     * 新增节假日管理
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/sys/festival/add','sys:festival:add')")
    @ApiOperation(value = "添加页面")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增节假日管理
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/sys/festival/save','sys:festival:add')")
    @Log(title = "节假日管理", describe = "新增节假日管理数据", type = BusinessType.ADD)
    @ApiOperation(value = "添加接口")
    public Result save(@RequestBody Festival festival) {
        return Result.decide(festivalService.save(festival));
    }

    /**
     * 修改节假日管理
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/sys/festival/edit','sys:festival:edit')")
    @ApiOperation(value = "修改页面")
    public ModelAndView edit(Long id, ModelMap map) {
        Festival festival =festivalService.getById(id);
        map.put("festival", festival);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改节假日管理
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/sys/festival/update','sys:festival:edit')")
    @Log(title = "节假日管理", describe = "修改节假日管理数据", type = BusinessType.EDIT)
    @ApiOperation(value = "修改接口")
    public Result update(@RequestBody Festival festival) {
        return Result.decide(festivalService.updateById(festival));
    }

    /**
     * 删除节假日管理
     */
    @ResponseBody
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/sys/festival/batchRemove','sys:festival:del')")
    @Log(title = "节假日管理", describe = "删除节假日管理数据", type = BusinessType.REMOVE)
    @ApiOperation(value = "批量删除接口")
    public Result batchRemove(String ids) {
        return Result.decide(festivalService.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 删除节假日管理
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/sys/festival/remove','sys:festival:del')")
    @Log(title = "节假日管理", describe = "删除节假日管理数据", type = BusinessType.REMOVE)
    @ApiOperation(value = "删除接口")
    public Result remove(@PathVariable("id") Long id) {
        return Result.decide(festivalService.removeById(id));
    }
}
